#include <ctime>
#include <unistd.h>
#include <cstdio>
#include <sys/types.h>
#include "BlockingQueue.hpp"

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* consumer(void* args)
{
    BlockingQueue<int>* bq = static_cast<BlockingQueue<int>*>(args);
    cout << (void*)bq << endl;
    sleep(1);

    while(1)
    {
        sleep(1);
        int data = 0;
        bq->pop(&data);
        //std::cout << "消费任务:" << data << std::endl;
        printf("消费任务：%d\n",data);
    }

    return nullptr;
}

void* productor(void* args)
{
    BlockingQueue<int>* bq = static_cast<BlockingQueue<int>*>(args);
    while(1)
    {
        sleep(1);
        int x = rand() % 10 + 1;
        bq->push(x);
        //std::cout << "生产任务:" << x << std::endl;
        printf("生产任务：%d\n",x);
    }

    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr));
    BlockingQueue<int>* bq = new BlockingQueue<int>();
    cout << (void*)bq << endl;
    sleep(1);
    pthread_t c,p;
    pthread_create(&p,nullptr,productor,bq);
    pthread_create(&c,nullptr,consumer,bq);

    pthread_join(c,nullptr);
    pthread_join(p,nullptr);
    
    delete bq;

    return 0;
}
